<?php

$loader->requireOnce('includes/AutoSuggest/AutoSuggestEngine.class.php');

/**
 * Controler for auto suggest.
 * @package modules.dept_pharmacy.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_AutoSuggest extends Controller {
    private $logger ;
    private $autoSuggestEngine;
    function __construct($template_mod = "general") {
        parent::Controller();
        $this->logger = LoggerManager::getLogger('controllers.C_AutoSuggest');
        $this->template_mod = $template_mod;
        $this->autoSuggestEngine = new AutoSuggestEngine();

    }
    public function actionDrugMaterialAutoSuggestAjax() {
        $this->logger->info('Start actionDrugMaterialAutoSuggestAjax');
        
        $autoSuggest = $this->autoSuggestEngine->getAutoSuggest(AutoSuggestEngine::DRUG_MATERIAL);
        $enumItemType = '';
        if (isset($_GET['enumItemType'])) {
            $enumItemType = $_GET['enumItemType'];
        }
        $filter = array( 'enum_item_type' => $enumItemType);
        $needle = strtolower($_GET["q"]);
        if (!$needle) return;
        
        $result = $autoSuggest->suggest($filter, $needle);
        $this->logger->info('Finish actionDrugMaterialAutoSuggestAjax');
        $arrEnumUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);
         
        foreach ($result as $item) {
            if (strpos(strtolower($item['proprietary_name']), $needle) !== false) {
                $unitText = '';
                if (isset($arrEnumUnit[$item['enum_unit_import_sell']])) {
                    $unitText = $arrEnumUnit[$item['enum_unit_import_sell']];
                }
                echo $item['proprietary_name']
                . "|"
                . $item['drug_material_id']
                . "|" . $unitText
                . "|" . $item['enum_unit_import_sell'] . "\n";
            }
        }

    }
    
       
    public function actionDrugMaterialAutoSuggest4DoctorAjax() {
        $this->logger->info('Start actionDrugMaterialAutoSuggest4DoctorAjax');
        
        $autoSuggest = $this->autoSuggestEngine->getAutoSuggest(AutoSuggestEngine::DRUG_MATERIAL);
        $enumItemType = '';
        if (isset($_GET['enumItemType'])) {
            $enumItemType = $_GET['enumItemType'];
        }
        $filter = array( 'enum_item_type' => $enumItemType);
        $needle = strtolower($_GET["q"]);
        if (!$needle) return;
        
        $result = $autoSuggest->suggest($filter, $needle, false);
        $this->logger->info('Finish actionDrugMaterialAutoSuggest4DoctorAjax');
        $arrEnumUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);
        $arrEnumUsage = Utils::getEnumArrayKey(EnumerationConstant::DM_DEFAULT_USAGE);
         
        foreach ($result as $item) {
            if (strpos(strtolower($item['proprietary_name']), $needle) !== false) {
                $dText = '';
                $quantityTitleEnumText = '';
                $usageTitleEnumText = '';
                if (isset($arrEnumUsage[$item['usage_title_enum']])) {
                    $usageTitleEnumText = $arrEnumUsage[$item['usage_title_enum']];
                }
                if (isset($arrEnumUnit[$item['dosage_title_enum']])) {
                    $dText = $arrEnumUnit[$item['dosage_title_enum']];
                }
                if (isset($arrEnumUnit[$item['quantity_title_enum']])) {
                    $quantityTitleEnumText = $arrEnumUnit[$item['quantity_title_enum']];
                }
                echo $item['proprietary_name']
                . "|" . $item['drug_material_id']
                . "|" . $usageTitleEnumText
                . "|" . $item['usage_title_enum']
                . "|" . $item['usage_num']
                . "|" . $dText
                . "|" . $item['dosage_title_enum']
                . "|" . $item['dosage']
                . "|" . $item['time']
                . "|" . $quantityTitleEnumText
                . "|" . $item['quantity_title_enum']
                . "|" . $item['quantity_num']
                . "|" . L10nHelper::renderNumber($item['price'])
                . "\n";
            }
        }

    }
    public function actionAreaAutoSuggestAjax() {
        $this->logger->info('Start actionAreaAutoSuggestAjax');
        $autoSuggest = $this->autoSuggestEngine->getAutoSuggest(AutoSuggestEngine::AREA_CODE);
        
        $needle = strtolower($_GET["q"]);
        if (!$needle) {
            $this->logger->info('Finish actionAreaAutoSuggestAjax');
            return;
        }
        
        $result = $autoSuggest->suggest(array('nationality'=>$_GET['nationality']), $needle);
        $this->logger->info('Finish actionAreaAutoSuggestAjax');
            
        foreach ($result as $item) {
            echo $item['auto_suggest_code']
                . "|"
                . $item['ward_id']
                . "\n";
        }

    }


}
?>